'use strict'

const fs = require('fs')
const { Sequelize, DataType, Op } = require('sequelize')
const { db_dialect, db_host, db_username, db_password, db_database } = require('../config/db')

// 利用sequelize工具连接数据库 
// sequelize; pg pg-hstore
const sequelize = new Sequelize(db_database, db_username, db_password, {
    host: db_host,
    dialect: db_dialect
})

let files = fs.readdirSync(__dirname)
let resFiles = files.filter(item => {
    return item.endsWith('.js') && item != 'index.js'
})

let resObj = {}
resFiles.forEach(item => {
    let modelName = item.replace('.js', '')
    let tmpObj = require(__dirname + '/' + item)
    let tableName = modelName.toLowerCase()
    resObj[modelName] = sequelize.define(tableName, tmpObj)
})
console.log(resObj);

resObj.Op=Op

resObj.sync=async ()=>{
    sequelize.sync({force:true}).then(()=>{
        resObj.Shopping.bulkCreate([
            {
                name:'短袖',
                price:88,
                supplier:'以纯'
            },
            {
                name:'短裤',
                price:88,
                supplier:'鸿星尔克'
            },
        ])
    })
}

module.exports = resObj